package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2022-11-10 10:04 星期四
 * description:
 */
@RestController
@RequestMapping("/api/hosp/hospital")
public class HospitalApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 首页上查询医院列表
     */
    @GetMapping("{page}/{limit}")
    public R index(@PathVariable Integer page, @PathVariable Integer limit, HospitalQueryVo hospitalQueryVo){
        Page<Hospital> pages = hospitalService.selectPage(page, limit, hospitalQueryVo);
        return R.ok().data("pages",pages);
    }


    //根据输入的医院名称进行模糊查询
    @GetMapping("findByHosname/{hosname}")
    public R findByHsoname(@PathVariable String hosname){List<Hospital> list = hospitalService.getByHosname(hosname);
        return R.ok().data("list",list);
    }

    @GetMapping("{hoscode}")
    public R item(@PathVariable String hoscode){
        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(20001,"医院编号");
        }
        Map<String,Object> map = hospitalService.item(hoscode);
        return R.ok().data(map);
    }

    @Autowired
    private DepartmentService departmentService;

    //获取科室列表
    @GetMapping("department/{hoscode}")
    public R index(@PathVariable String hoscode){
        List<DepartmentVo> list = departmentService.findDeptTree(hoscode);
        return R.ok().data("list",list);
    }


    //跳转到详情页面 该页面跳转到该接口
    //日期列表
    @GetMapping("auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public R getBookingSchedule(@PathVariable Integer page,@PathVariable Integer limit,
                                @PathVariable String hoscode,@PathVariable String depcode){

        //map--->List<BookingScheduleRuleVo> (每个日期封装成一个BookingScheduleRuleVo对象)
        Map<String,Object> map = scheduleService.getBookingSchedule(page,limit,hoscode,depcode);
        return R.ok().data(map);
    }

    @GetMapping("auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    public R getScheduleDetail(@PathVariable String hoscode,@PathVariable String depcode,
                               @PathVariable String workDate){

        List<Schedule> list = scheduleService.getDetailSchedule(hoscode, depcode, workDate);
        return R.ok().data("scheduleList",list);

    }

    //点击剩余按钮 查询排班详情
    @GetMapping("getSchedule/{id}")
    public R getSchedule(@PathVariable String id) {
        Schedule schedule = scheduleService.getById(id);
        return R.ok().data("schedule",schedule);
    }

    @GetMapping("inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(@PathVariable("scheduleId") String scheduleId) {
        return scheduleService.getScheduleOrderVo(scheduleId);
    }

    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("getApiUrlByHoscode/{hoscode}")
    public String getApiUrlByHoscode(@PathVariable("hoscode") String hoscode){
        HospitalSet hospitalSet = hospitalSetService.getUrlByHoscode(hoscode);
        return hospitalSet.getApiUrl();
    }

}
